<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/11/21
 * Time: 11:52
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\api\controller\v1;

use app\api\controller\Base;
use app\common\business\Address as BusObj;
use app\common\lib\Show;
use app\common\lib\Status;
use think\App;
use think\Exception;

class Address extends Base
{
    protected BusObj $busObj;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->busObj = new BusObj();
    }

    public function setAddressData()
    {
        $openid = $this->request->param('openid', '', 'trim');
        $school = $this->request->param('school', 0, 'intval');
        $number = $this->request->param('number', '', 'trim');
        $relname = $this->request->param('relname', '', 'trim');
        $address = $this->request->param('address', '', 'trim');
        $addressLat = $this->request->param('addressLat', '', 'trim');
        $addressLng = $this->request->param('addressLng', '', 'trim');
        $sex = $this->request->param('sex', '', 'trim');
        $mobile = $this->request->param("mobile", '', 'trim');
        $data = [
            "openid" => $openid,
            "school" => $school,
            "number" => $number,
            "relname" => $relname,
            "sex" => $sex,
            "mobile" => $mobile,
            "address" => $address,
            "addressLat" => $addressLat,
            "addressLng" => $addressLng
        ];
        try {
            $res = $this->busObj->add($data);
        } catch (\Exception $e) {
            return Show::json_show(Status::mini_error, $e->getMessage());
        }
        if ($res) {
            return Show::json_show(Status::mini_normal, "操作成功");
        } else {
            return Show::json_show(Status::mini_error, "操作失败");
        }
    }

    public function getAddress()
    {
        $openid = $this->request->param('openid', '', 'trim');
        $limit = $this->request->param('limit', 10, 'intval');
        $page = $this->request->param('page', 1, 'intval');
        $where = [
            ['openid', '=', $openid],
            ["status", '=', Status::status_normal]
        ];
        try {
            $lists = $this->busObj->getIndexLimitPage($limit, $where, 'id desc', '*,school as school_id');
        } catch (\Exception $e) {
            return Show::json_show(Status::mini_error, $e->getMessage());
        }
        return Show::json_show(Status::mini_normal, "ok", $lists);
    }

    public function deleteData()
    {
        $id = $this->request->param('id', 0, 'intval');
        if ($id == 0) {
            return Show::json_show(Status::mini_error, "内部发生错误");
        }
        try {
            $res = $this->busObj->updateByData($id, [
                "status" => Status::mysql_delete
            ]);
        } catch (\Exception $e) {
            return Show::json_show(Status::mini_error, $e->getMessage());
        }
        if ($res) {
            return Show::json_show(Status::mini_normal, "操作成功");
        } else {
            return Show::json_show(Status::mini_error, "操作失败");
        }
    }

    public function getAddressByList()
    {
        $openid = $this->request->param('openid', '', 'trim');
        try {
            $data = $this->busObj->getAddressByOpenidList($openid,'*,address as name');
        } catch (\Exception $e) {
            return Show::json_show(Status::mini_error, $e->getMessage());
        }
        return Show::json_show(Status::mini_normal, "ok", $data);
    }
}